{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 语言基础知识"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## IDE  \n",
    ">* IDLE（IDLE是是一个纯 Python 下自带的简洁的集成开发环境，偶尔执行一些简单的语句、测试。）  \n",
    ">* jupyter（1、jupyter是一个基于web的IDE（集成开发环境）。2、兼具脚本操作和交互式操作的特性。3、笔记式编辑代码和运行，便于调试，便于保存。） \n",
    ">* pycharm（中大型项目中方便组织较多文件，功能更为丰富。）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-3-079b61183158>, line 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-3-079b61183158>\"\u001b[1;36m, line \u001b[1;32m4\u001b[0m\n\u001b[1;33m    \"JUPYTE\":[\"操作，75\",\"意义（80）\"],\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "python_base={\n",
    "    \"IDE\":{  \n",
    "               \"pycharm\":[\"操作，50\",\"意义,85\"]\n",
    "               \"JUPYTE\":[\"操作，75\",\"意义（80）\"],\n",
    "              \"IDLE\":[\"操作，100（90）\",\"意义100（80）\"],\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模块类型  \n",
    ">* 标准库：(1)时间戳 指格林威治时间1970年01月01日00时00分00秒（北京时间1970年01月01日08时00分00秒）到现在等秒数（UNIX诞生生元年）。(2）格式化的时间字符串按照自己等格式定义的字符串例如：（2019-08-25 08:30:56）等。（3）元组（struct_time）共九个元素。由于Python的time模块实现主要调用C库，所以各个平台可能有所不同。             \n",
    ">* 自定义模块意义：1、从文件级别组织程序，更方便管理。2、拿来主义，提升开发效率。\n",
    ">* 开源模块：开源模块是每一位开发者编译完成后上传到开放平台供别人使用的模块，可以在https://pypi.python.org/pypi下载到更多的模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_module={\n",
    "       \"Standard library\":{  \n",
    "               \"time and datetime\":[\"import time，100（80）\",\"意义100（75）\"],\n",
    "               \"custom\":[\"import（def），85\",\"意义80\"],\n",
    "               \"open source\":[\"操作，80\",\"意义75\"]\n",
    "                     }                      \n",
    "               }"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量、赋值、数据类型  \n",
    ">* 变量用于存储要在计算机程序中引用和操作的信息。它们还提供了一种用描述性名称标记数据的方法，以便读者和我们自己能够更清楚地理解我们的程序。将变量看作包含信息的容器是有帮助的。它们的唯一目的是在内存中标记和存储数据。然后可以在整个程序中使用这些数据。  \n",
    ">* 规则：1.变量名只能是 字母、数字或下划线的任意组合。2.变量名的第一个字符不能是数字。以下关键字不能声明为变量名'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda','not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield'\"。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_variable={\n",
    "    \"variable\":{  \n",
    "               \"variable\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}\n",
    "#examlpe\n",
    "student_number = 49\n",
    "dog_name = \"Lucky\" #_类型，比较常用的类型\n",
    "Dogname = \"Lucky\" #首字母大写 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hehe hehe\n",
      "hehe\n"
     ]
    }
   ],
   "source": [
    "#变量的赋值\n",
    "name = \"hehe\"\n",
    "name2 = name\n",
    "print(name,name2)\n",
    "name = \"xiaoxiong\"\n",
    "print(name2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据类型  \n",
    ">* 列表  \n",
    ">* 元组  \n",
    ">* 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_data={\n",
    "    \"data\":{  \n",
    "               \"data\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['lele', 786, 2.23, 'xiaotian', 70.2]\n",
      "lele\n",
      "[786, 2.23]\n",
      "[2.23, 'xiaotian', 70.2]\n",
      "[123, 'xiaoming', 123, 'xiaoming']\n",
      "['lele', 786, 2.23, 'xiaotian', 70.2, 123, 'xiaoming']\n"
     ]
    }
   ],
   "source": [
    "# 列表\n",
    "#List（列表） 是 Python 中使用最频繁的数据类型。\n",
    "#列表可以完成大多数集合类的数据结构实现。它支持字符，数字，字符串甚至可以包含列表（即嵌套）。\n",
    "#列表用 [ ] 标识，是 python 最通用的复合数据类型。\n",
    "\n",
    "\n",
    "list = [ 'lele', 786 , 2.23, 'xiaotian', 70.2 ]\n",
    "tinylist = [123, 'xiaoming'] \n",
    "print (list)               # 输出完整列表\n",
    "print (list[0])            # 输出列表的第一个元素\n",
    "print (list[1:3])          # 输出第二个至第三个元素 \n",
    "print (list[2:])           # 输出从第三个开始至列表末尾的所有元素\n",
    "print (tinylist * 2)       # 输出列表两次\n",
    "print (list + tinylist)    # 打印组合的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('lele', 786, 2.23, 'xiaoxiong', 70.2)\n",
      "lele\n",
      "(786, 2.23)\n",
      "(2.23, 'xiaoxiong', 70.2)\n",
      "(123, 'xiaoxiong', 123, 'xiaoxiong')\n",
      "('lele', 786, 2.23, 'xiaoxiong', 70.2, 123, 'xiaoxiong')\n"
     ]
    }
   ],
   "source": [
    "# 元组\n",
    "# 元组是另一个数据类型，类似于 List（列表）。\n",
    "# 元组用 () 标识。内部元素用逗号隔开。但是元组不能二次赋值，相当于只读列表。\n",
    "\n",
    "tuple = ( 'lele', 786 , 2.23, 'xiaoxiong', 70.2 )\n",
    "tinytuple = (123, 'xiaoxiong')\n",
    "print (tuple)               # 输出完整元组\n",
    "print (tuple[0])            # 输出元组的第一个元素\n",
    "print (tuple[1:3])          # 输出第二个至第四个（不包含）的元素 \n",
    "print (tuple[2:])           # 输出从第三个开始至列表末尾的所有元素\n",
    "print (tinytuple * 2)       # 输出元组两次\n",
    "print (tuple + tinytuple)   # 打印组合的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is one\n",
      "This is two\n",
      "{'name': 'lele', 'code': 6734, 'dept': 'sales'}\n",
      "dict_keys(['name', 'code', 'dept'])\n",
      "dict_values(['lele', 6734, 'sales'])\n"
     ]
    }
   ],
   "source": [
    "# 字典\n",
    "# 字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合，字典是无序的对象集合。\n",
    "# 两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。\n",
    "# 字典用\"{ }\"标识。字典由索引(key)和它对应的值value组成。\n",
    "\n",
    "dict = {}\n",
    "dict['one'] = \"This is one\"\n",
    "dict[2] = \"This is two\" \n",
    "tinydict = {'name': 'lele','code':6734, 'dept': 'sales'} \n",
    "print (dict['one'])          # 输出键为'one' 的值\n",
    "print (dict[2])              # 输出键为 2 的值\n",
    "print (tinydict)             # 输出完整的字典\n",
    "print (tinydict.keys())      # 输出所有键\n",
    "print (tinydict.values())    # 输出所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数字  \n",
    ">* 数字数据类型用于存储数值。他们是不可改变的数据类型，这意味着改变数字数据类型会分配一个新的对象。当你指定一个值时，Number 对象就会被创建。  \n",
    ">* Python支持四种不同的数字类型：\n",
    "int（有符号整型）\n",
    "long（长整型[也可以代表八进制和十六进制]）\n",
    "float（浮点型）\n",
    "complex（复数）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串  \n",
    ">* 字符串或串(String)是由数字、字母、下划线组成的一串字符。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n",
      "H\n",
      "llo\n",
      "llo World!\n",
      "Hello World!Hello World!\n",
      "Hello World!TEST\n"
     ]
    }
   ],
   "source": [
    "str = 'Hello World!'\n",
    " \n",
    "print(str)           # 输出完整字符串\n",
    "print(str[0])        # 输出字符串中的第一个字符\n",
    "print(str[2:5])      # 输出字符串中第三个至第六个之间的字符串\n",
    "print(str[2:])       # 输出从第三个字符开始的字符串\n",
    "print(str * 2)       # 输出字符串两次\n",
    "print(str + \"TEST\")  # 输出连接的字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter amount: 12\n",
      "Discount 0.6000000000000001\n",
      "Net payable: 11.4\n"
     ]
    }
   ],
   "source": [
    "# if...else的用法\n",
    "amount = int(input(\"Enter amount: \"))\n",
    "\n",
    "if amount<1000:\n",
    "   discount = amount*0.05\n",
    "   print (\"Discount\",discount)\n",
    "else:\n",
    "   discount = amount*0.10\n",
    "   print (\"Discount\",discount)\n",
    "\n",
    "print (\"Net payable:\",amount-discount)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter amount: 10\n",
      "Discount 0.5\n",
      "Net payable: 9.5\n"
     ]
    }
   ],
   "source": [
    "# elif语句用法\n",
    "amount = int(input(\"Enter amount: \"))\n",
    "\n",
    "if amount<1000:\n",
    "   discount = amount*0.05\n",
    "   print (\"Discount\",discount)\n",
    "elif amount<5000:\n",
    "   discount = amount*0.10\n",
    "   print (\"Discount\",discount)\n",
    "else:\n",
    "   discount = amount*0.15\n",
    "   print (\"Discount\",discount)\n",
    "\n",
    "print (\"Net payable:\",amount-discount)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for 循环 内置函数range()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_for={\n",
    "    \"for,range\":{  \n",
    "               \"for\":[\"操作，100（80）\",\"意义100（75）\"],\n",
    "               \"内置range\":[\"操作，85\",\"意义，75\"],\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "i\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "#循环迭代字符串变量\n",
    "for ch in \"Hi!\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n",
      "Head First Rocks!\n"
     ]
    }
   ],
   "source": [
    "#迭代指定的次数\n",
    "for num in range(5):\n",
    "    print('Head First Rocks!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## range循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l\n",
      "e\n",
      "l\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "x = 'lele'\n",
    "for i in range(len(x)) :\n",
    "    print(x[i])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## for循环与内置range  \n",
    ">* 可进行循环遍历，进行搜索、提取等功能，非常强大！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not an odd second.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59]\n",
    "\n",
    "for i in range(1):\n",
    "    right_this_second = datetime.today().second\n",
    "    if right_this_second in odds:\n",
    "        print(\"This second seems a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd second.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# random模块  \n",
    ">* 生成随机数、随机字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_random={\n",
    "    \"random\":{  \n",
    "               \"random\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "0.9559160608122282\n",
      "3.2320615377215987\n",
      "o\n",
      "27\n",
      "[3, 7, 1, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "print( random.randint(1,10) )        # 产生 1 到 10 的一个整数型随机数  \n",
    "print( random.random() )             # 产生 0 到 1 之间的随机浮点数\n",
    "print( random.uniform(1.1,5.4) )     # 产生  1.1 到 5.4 之间的随机浮点数，区间可以不是整数\n",
    "print( random.choice('tomorrow') )   # 从序列中随机选取一个元素\n",
    "print( random.randrange(1,100,2) )   # 生成从1到100的间隔为2的随机整数\n",
    "\n",
    "a=[1,3,5,6,7]                # 将序列a中的元素顺序打乱\n",
    "random.shuffle(a)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元组  \n",
    ">* Python的元组与列表类似，不同之处在于元组的元素不能修改。\n",
    ">* 元组使用小括号，列表使用方括号。\n",
    ">* 元组创建很简单，只需要在括号中添加元素，并使用逗号隔开即可。\n",
    ">* 使用场景：元组的使用场景：1.函数的参数和返回值，一个函数可以接收任意多个参数，一次返回多个数据；2.格式字符串；3.让列表不可以被修改，保护数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_tuple={\n",
    "    \"tuple\":{  \n",
    "               \"tuple\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## while循环\n",
    ">* Python 编程中 while 语句用于循环执行程序，即在某条件下，循环执行某段程序，以处理需要重复处理的相同任务。\n",
    ">* 重点关注:需要加入循环结束条件，break。  \n",
    ">* ![流程图](https://www.runoob.com/wp-content/uploads/2013/11/886A6E10-58F1-4A9B-8640-02DBEFF0EF9A.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_while={\n",
    "    \"while\":{  \n",
    "               \"while\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "good bye... 0\n",
      "good bye... 1\n",
      "good bye... 2\n",
      "good bye... 3\n",
      "good bye... 4\n",
      "good bye... 5\n",
      "good bye... 6\n",
      "good bye... 7\n",
      "good bye... 8\n",
      "good bye... 9\n"
     ]
    }
   ],
   "source": [
    "count = 0\n",
    "while True:\n",
    "    print(\"good bye...\",count)\n",
    "    count +=1\n",
    "    if count == 10:  #加入结束条件\n",
    "        break               #跳出循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：········\n",
      "<class 'int'>\n",
      "猜一猜数字是多少：3\n",
      "<class 'int'>\n",
      "你猜的数字太小啦，往大里猜哈\n",
      "猜一猜数字是多少：4\n",
      "<class 'int'>\n",
      "你猜的数字太小啦，往大里猜哈\n",
      "猜一猜数字是多少：5\n",
      "<class 'int'>\n",
      "你猜的数字太小啦，往大里猜哈\n",
      "猜一猜数字是多少：6\n",
      "<class 'int'>\n",
      "哇呜~你猜对啦！你真是猜数字小天才\n"
     ]
    }
   ],
   "source": [
    "#while与if结合\n",
    "import getpass\n",
    "card = int(getpass.getpass(\"请输入数字：\"))\n",
    "print(type(card))\n",
    "count = 0\n",
    "\n",
    "while True:\n",
    "    if count <8:\n",
    "        guess = int(input(\"猜一猜数字是多少：\"))\n",
    "        print(type(guess))\n",
    "        if card == guess:\n",
    "            print(\"哇呜~你猜对啦！你真是一个小天才\")\n",
    "            break\n",
    "        elif card > guess:\n",
    "            print(\"你猜的数字太小啦，往大里猜哈\")\n",
    "        else:\n",
    "            print(\"你猜的数字太大啦，往小猜哈\")\n",
    "    else:\n",
    "        print(\"猜这么久都没猜出来，你是猪吗！\")\n",
    "        break\n",
    "    count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 集合\n",
    ">* Python还包括用于集合的数据类型。集合是没有重复元素的无序集合。基本用途包括成员资格测试和消除重复条目。集合对象还支持数学运算，例如并集，交集，差和对称差。\n",
    ">* 花括号或set()函数可用于创建集合。注意：要创建一个空集，您必须使用set()，而不是{}; 后者将创建一个空字典，这是我们将在下一节中讨论的数据结构。\n",
    ">* 集合不允许有重复，集合是用大括号包围，集合中只有逗号，绝对没有冒号。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_set={\n",
    "    \"set\":{  \n",
    "               \"set\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'pear', 'apple', 'orange', 'banana'}\n",
      "list_2: {1, 2, 3, 4, 5, 6, 7} \n",
      " <class 'set'>\n",
      "list_3: {0, 1, 66, 3, 4, 7, 8}\n",
      "union： {0, 1, 2, 3, 4, 5, 6, 7, 66, 8}\n",
      "union： {0, 1, 66, 3, 4, 2, 5, 7, 8, 6}\n",
      "{2, 5, 6}\n",
      "{0, 8, 66}\n",
      "False\n",
      "True\n",
      "{0, 66, 2, 5, 6, 8}\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)                      # 这里演示的是去重功能\n",
    "\n",
    "# 利用sorted()与list()函数将一个集合转化为一个有序的列表\n",
    "a = {\"a\",\"b\",\"c\",\"d\",\"e\"}\n",
    "b = sorted(list(a))\n",
    "print(b)\n",
    "\n",
    "# union合并集合\n",
    "a = {\"a\",\"b\",\"c\",\"d\",\"e\"}\n",
    "word = \"lele\"\n",
    "u = a.union(set(word))\n",
    "print(u)\n",
    "\n",
    "# defference()告诉你哪些不是共有的元素\n",
    "a = {\"a\",\"b\",\"c\",\"d\",\"e\"}\n",
    "word = \"lele\"\n",
    "b = a.differerce(set(word))\n",
    "print(b)\n",
    "\n",
    "# intersection()报告共同对象\n",
    "a = {\"a\",\"b\",\"c\",\"d\",\"e\"}\n",
    "word = \"lele\"\n",
    "b = a.intersection(set(word))\n",
    "print(b)\n",
    "\n",
    "# -*- coding:utf-8 -*-\n",
    "list_1 = [1,2,5,6,7,2,3,2,4,4]\n",
    "\n",
    "list_2 = set(list_1)\n",
    "list_3 = set([1,3,4,0,8,66,7])\n",
    "\n",
    "print(\"list_2:\",list_2,\"\\n\",type(list_2))\n",
    "print(\"list_3:\",list_3)\n",
    "# 交集\n",
    "# print(list_2.intersection(list_3))\n",
    "# print(list_2 & list_3)\n",
    "\n",
    "# 并集 思考，list_3.union(list_2)\n",
    "print(\"union：\",list_2.union(list_3))\n",
    "print(\"union：\",list_3.union(list_2))\n",
    "# print(list_2 | list_3)\n",
    "\n",
    "# 差集\n",
    "print(list_2.difference(list_3))\n",
    "# print(list_2 - list_3)\n",
    "# 22\n",
    "print(list_3.difference(list_2))\n",
    "# print(list_3 - list_2)\n",
    "\n",
    "# 子集\n",
    "print(list_2.issubset(list_3))  #子集\n",
    "# print(list_2.issuperset(list_3))  #父集\n",
    "list_4 = set([2,3])\n",
    "print(list_4.issubset(list_2))\n",
    "\n",
    "# 对称差集 两个互相做差集，然后再∪（union）\n",
    "print(list_2.symmetric_difference(list_3))\n",
    "\n",
    "# set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数  \n",
    ">* 函数，编程语言中函数的定义（计算机）：函数是逻辑结构化和过程化的一种编程方法。\n",
    ">* 定义一个函数：  \n",
    "1、函数代码块以 def 关键词开头，后接函数标识符名称和圆括号 ()。  \n",
    "2、任何传入参数和自变量必须放在圆括号中间，圆括号之间可以用于定义参数。  \n",
    "3、函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。  \n",
    "4、函数内容以冒号 : 起始，并且缩进。  \n",
    "5、return [表达式] 结束函数，选择性地返回一个值给调用方，不带表达式的 return 相当于返回 None。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "python_def={\n",
    "    \"def\":{  \n",
    "               \"def\":[\"操作，100（80）\",\"意义100（75）\"]\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World!\n"
     ]
    }
   ],
   "source": [
    "# 例子\n",
    "def hello() :\n",
    "    print(\"Hello World!\")\n",
    "\n",
    "hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 向函数传递信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,Lele!\n"
     ]
    }
   ],
   "source": [
    "def greet_user(username):    \n",
    "     \"\"\"显示简单的问候语\"\"\"    \n",
    "     print(\"hello,\"+username.title()+\"!\")         \n",
    "greet_user(\"lele\") \n",
    "# 代码greet_user(\"lele\")调用函数greet_user()时，向它传递执行print()语句所需要的信息username。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have ahamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have acat.\n",
      "Mycat's name is Python.\n"
     ]
    }
   ],
   "source": [
    "# 位置实参（基于实参的位置）\n",
    "# 位置实参的顺序很重要，请确认函数调用中实参的顺序与函数定义形参的顺序一致\n",
    "\n",
    "def describe_pet(animal_type,pet_name):\n",
    "     \"\"\"显示宠物信息\"\"\"\n",
    "     print(\"\\nI have a\" + animal_type + \".\")\n",
    "     print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    " \n",
    "describe_pet('hamster','harry')\n",
    "describe_pet('cat','python')  #函数调用多次是一种效率极高的工作方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数\n",
    "\n",
    "def describe_pet(pet_name,animal_type=\"dog\",):\n",
    "     \"\"\"显示宠物信息\"\"\"\n",
    "     print(\"\\nI have a \" + animal_type + \".\")\n",
    "     print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")\n",
    " \n",
    "describe_pet(pet_name='harry',animal_type='hamster')  #关键字实参\n",
    "describe_pet(pet_name='python',animal_type='cat')  #关键字实参"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 默认值\n",
    ">* 编写函数时，可以给形参指定默认值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字:  lele\n",
      "年龄:  50\n",
      "------------------------\n",
      "名字:  lele\n",
      "年龄:  35\n"
     ]
    }
   ],
   "source": [
    "def printinfo( name, age = 35 ):\n",
    "   \"打印任何传入的字符串\"\n",
    "   print (\"名字: \", name)\n",
    "   print (\"年龄: \", age)\n",
    "   return\n",
    " \n",
    "#调用printinfo函数\n",
    "printinfo( age=50, name=\"lele\" )\n",
    "print (\"------------------------\")\n",
    "printinfo( name=\"lele\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 向参数传递列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Hannah!\n",
      "Hello, Ty!\n",
      "Hello, Margot!\n"
     ]
    }
   ],
   "source": [
    "def greet_users(names):\n",
    "      \"\"\"向列表中的每位用户都发出简单的问候\"\"\"\n",
    "      for name in names:\n",
    "          msg = \"Hello, \"+name.title() +\"!\"\n",
    "          print(msg)\n",
    "  \n",
    "usernames = ['hannah','ty','margot']\n",
    "greet_users(usernames)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数返回值\n",
    ">* 函数返回的值被称为函数的返回值；  \n",
    "在函数中，可用return语句将值返回到调用函数的代码行；  \n",
    "返回值能将你程序的大部分繁重工作移到函数中去完成，从而简化主程序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jimi Hendrix\n"
     ]
    }
   ],
   "source": [
    "def get_formatted_name(first_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+ last_name\n",
    "    return full_name.title()\n",
    "\n",
    "musician = get_formatted_name('jimi','hendrix') #调用返回值的函数时，需要提供一个变量，用于存储返回的值\n",
    "print(musician)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jimi Lee Hendrix\n",
      "Jimi Hendrix Lee\n",
      "Jimi Hendrix\n"
     ]
    }
   ],
   "source": [
    "# 让实参变可选\n",
    "\n",
    "def get_formatted_name(first_name,middle_name,last_name):\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    full_name = first_name + ' '+middle_name+' '+ last_name\n",
    "    return full_name.title()\n",
    "\n",
    "musician = get_formatted_name('jimi','lee','hendrix') #有些人有中间名\n",
    "print(musician)\n",
    "\n",
    "# 有些人不一定有中间名，那么对函数进行优化：\n",
    "def get_formatted_name(first_name,last_name,middle_name=''):  #复习关键字实参放后面\n",
    "    \"\"\"返回整洁的姓名\"\"\"\n",
    "    if middle_name:\n",
    "        full_name = first_name + ' '+middle_name+' '+ last_name\n",
    "    else:\n",
    "        full_name = first_name + ' ' + last_name\n",
    "    return full_name.title()\n",
    "\n",
    "musician = get_formatted_name('jimi','lee','hendrix')\n",
    "musician1 = get_formatted_name('jimi','hendrix')\n",
    "print(musician)\n",
    "print(musician1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 函数返回多个值\n",
    ">* 函数可以返回任意类型的值，包括列表、字典和集合等较为复杂的数据结构。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']\n"
     ]
    }
   ],
   "source": [
    "# 返回列表\n",
    "def list(self):\n",
    "    a = []\n",
    "    for i in self:\n",
    "        a.append(i)\n",
    "    return a        \n",
    "\n",
    "list_test = list(\"hello world\")\n",
    "print(list_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def search4letters(phrase:str,letters:str)->set:\n",
    "    \"\"\"return a set of the 'letters' found in 'phrase'. \"\"\"\n",
    "    logger(\"search4letters\")\n",
    "    return set(letters).intersection(set(phrase))\n",
    "\n",
    "def logger(source):\n",
    "    print(\"from %s\"%source)\n",
    "help(search4letters)\n",
    "\n",
    "print(search4letters('hitch-hiker','aeiou'))\n",
    "print(search4letters('galaxy','xyz'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "5\n",
      "2\n",
      "1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def calc(n):\n",
    "    print(n)\n",
    "    if int(n/2) == 0:\n",
    "        return n\n",
    "    return calc(int(n/2))\n",
    "\n",
    "calc(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "229.188px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
